views:

431

answers:

5

I have two threads in my application. Is it possible to execute both the threads simultaneously without sleeping any thread?

+1  A: 

Yes, if you have multiple processors or multi-core processors. One thread will run in one core.

Naveen
Threads aren't guaranteed to run on another processor or core, even if you have more than one.
Kristopher Ives
That may be true, but it is guaranteed that on a single core machine, only one thread will be running (ignoring hyperthreads - those make it at least a pseudo-multicore machine). With a multicore machine, there is a possibility that the threads really will run concurrently - but no guarantee.
Jonathan Leffler
A: 

Read this paper .

Synchronization: Concurrency

Night Walker
+3  A: 

C does not, itself, have any means to do multi-threaded code.

However, POSIX has libraries that allow you to work with threads in C.

One good article about this topic is How to write multi-threaded software in C and C++.

Chip Uni
Most of the times OS also provides libraries for multi-threading. In C on windows, You can use _beginthreadex() to create threads in your application or even CreateThread. But _beginthreadex() is preferred.
Kavitesh Singh
+8  A: 

You can run the threads parallel in your application especially if they are not waiting on each other for some inputs or conditions. For example: One thread may be parsing a file and other maybe playing a song in your application.

Generally OS takes care of the thread time slicing. So at the application level it would look like these threads are running parallel but the OS does the time slicing giving each thread certain execution time.

With multi-core processors/cores it is possible to run the threads parallel in realtime, however the OS decides which threads to run unless you specifically code at lower level to ensure which threads you want to run in parallel.

Kavitesh Singh
+1 for mentioning multiple cores.
Jonathan Leffler
+4  A: 

As others have mentioned, with multiple cores it is possible, but, it depends on how the OS decides to distribute the threads. You don't have any control, that I have seen, on dictating where each thread is ran.

For a really good tutorial, with some nice explanation and pictures you can look at this page, with code as to how to do multi-threading using the POSIX library.

http://www.pathcom.com/~vadco/parallel.html

The time slice for sleep is hard to see, so your best bet is to test it out, for example, have your two threads begin to count every millisecond, and see if the two are identical. If they are not, then at least one is going to sleep by the cpu.

Most likely both will go to sleep at some time, the test is to see how much of a difference there is between the two threads.

Once one thread blocks, either waiting to send data, or waiting to receive, it will be put to sleep so that other threads can run, so that the OS can continue to make certain everything is working properly.

James Black
On some platforms, you can control where each thread runs using an affinity API, such as SetThreadAffinityMask() on Windows and sched_setaffinity() on Linux.
bk1e
@bk1e - Thank you, I have never heard of this, but it looks good for the instances where I want to override the OS on scheduling.
James Black
@bkl1e - from MSDN: Setting an affinity mask for a process or thread can result in threads receiving less processor time, as the system is restricted from running the threads on certain processors. In most cases, it is better to let the system select an available processor.
Kavitesh Singh
@Kavitesh Singh - If you are running an application that basically hogs the computer, then this may make sense, but in most cases I think it would be risky to do this, but it is an option that can be useful.
James Black