Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can threads be avoided?

I've read a lot recently about how writing multi-threaded apps is a huge pain in the neck, and have learned enough about the topic to understand, at least at some level, why it is so.

I've read that using functional programming techniques can help alleviate some of this pain, but I've never seen a simple example of functional code that is concurrent. So, what are some alternatives to using threads? At least, what are some ways to abstract them away so you needn't think about things like locking and whether a particular library's objects are thread-safe.

I know Google's MapReduce is supposed to help with the problem, but I haven't seen a succinct explanation of it.

Although I'm giving a specific example below, I'm more curious of general techniques than solving this specific problem (using the example to help illustrate other techniques would be helpful though).

I came to the question when I wrote a simple web crawler as a learning exercise. It works pretty well, but it is slow. Most of the bottleneck comes from downloading pages. It is currently single threaded, and thus only downloads a single page at a time. Thus, if the pages can be downloaded concurrently, it would speed things up dramatically, even if the crawler ran on a single processor machine. I looked into using threads to solve the issue, but they scare me. Any suggestions on how to add concurrency to this type of problem without unleashing a terrible threading nightmare?

like image 563
Tristan Havelick Avatar asked Dec 19 '08 22:12

Tristan Havelick


People also ask

How do I stop multithreading?

You can implement concurrent tasks by using coroutines. You then have to explicitly pass control (the cpu) to another coroutine. It won't be done automatically by an interrupt after a small delay.

How a thread can be terminated?

A thread automatically terminates when it returns from its entry-point routine. A thread can also explicitly terminate itself or terminate any other thread in the process, using a mechanism called cancelation.


1 Answers

The reason functional programming helps with concurrency is not because it avoids using threads.

Instead, functional programming preaches immutability, and the absence of side effects.

This means that an operation could be scaled out to N amount of threads or processes, without having to worry about messing with shared state.

like image 200
FlySwat Avatar answered Sep 29 '22 19:09

FlySwat