OpenMP questions regarding tasks and nowait - multithreading

Can anyone tell me the answers to these questions? I think the answer for question b) is that the program on the left schedules task beta() and immediately executes alpha() on the same thread, while the program on the right simply schedules beta() and alpha() for execution by other threads. (please correct me if I'm wrong)

It looks like your homework. So, I will not tell you the solution, but I give a hint. To answer a) consider all implied barriers.

Related

yielding from linux kernel

I have a real-time thread in Linux (3.4). Under certain conditions, I want it to relinquish control to other threads with the same priority, even if it hasn't finished using up its current timeslice. I was thinking of using the following code:
if (condition) {
resched_task();
cond_resched();
}
however, I don't see anyone else in the code doing this, making me think there's some other (better?) way of doing this. Is there a standard way to do this?
You can use the sched_yield() function to yield the rest of your time slice, as discussed here.
sched_yield() causes the calling thread to relinquish the CPU. The thread is moved to the end of the queue for its static priority and a new thread gets to run.
The question sounds like it's asking about kernel programming but the accepted answer is a function user mode API sched_yield(). I think the kernel answer to this question is schedule()

What are the main purposes for joining pthreads in Linux/UNIX?

I'm a student and I'm going over threads right now, and despite reading TLPI very carefully, I still don't have a good understanding as to why one might join two pthreads.
From what I've gleaned, it can be used either as a way for one thread to pass a return value to another OR it can be used as a waiting mechanism between threads. That said, it's entirely possible that I've misunderstood the entire point. Would someone mind explaining it a bit for me?
Threads are mainly used for parallel processing. Joining/Exiting threads means the work/purpose of the thread is fulfilled. When the purpose is fulfilled then the resources should be freed and made available to other threads/processes. Resources could be any of following:
Stack (as Basile Starynkevitch said)
Processor time
Opened files/Shared Memory/Any other resource locked/booked by the thread.
Joining threads can be done for just shifting the control also Or it might be done for transferring values as return values (as Michael Burr said).

Is it possible to know how much CPU usage the current thread is getting?

I am writing a task scheduler for offloaded tasks in a game engine, and I want it to tune itself based on a few heuristics.
Is it possible to know for how long the current thread has been executed between two points in time? I want to time how long tasks take to execute, and I would like that time to exclude thread switching for multiple reasons (its a more accurate measurement, plus it would be useful to know how much my threads are being switched out).
I would like a solution for linux but a windows solution would also be appreciated
Try to look at /proc/[pid]/task/[tid]/stat
Format is similar to /proc/[pid]/stat and explained here: http://www.kernel.org/doc/man-pages/online/pages/man5/proc.5.html
Example code from pthread_getcpuclockid() man page indicates that you could use clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts).

Detecting scheduled threads on linux

I need to detect whether a given thread is currently scheduled to a CPU or not. I am on a linux system with pthreads. Say that my code is running on a signal handler and I know that there are threads X and Y in my app, and from within my signal handler I want to check if X and Y are currently scheduled or not.
Thanks for your help.
In general if you have to ask this question, you are probably thinking incorrectly. Whatever answer you are going to get is going to be immediately outdated and whatever you plan to do about the answer is going to be subject to massive race conditions.
However, in theory you can look at /proc/getpid()/task/threadid/status and find out if a particular thread is running or not. Note this is very much a linux thing and any mapping between "threadid" and pthread_self()'s return code or pthread_create's pthread_t copy-out is very much not a standard.

Why was the method java.lang.Thread.join() named like that?

Does anybody know why the method join() member of a java.lang.Thread was named like that? Its javadoc is:
Waits for this thread to die.
When join is called on some thread calling thread is waiting for the other to die and continue execution. Supposedly calling thread will die as well, but still it's not clear why the author used this name.
It's a common name in threading - it's not like Java was the first to use it. (For example, that's what pthreads uses too.)
I guess you could imagine it like two people taking a walk - you join the other one and walk with them until you've finished, before going back to what you were doing. That sort of analogy may have been the original reason, although I agree it's not exactly intuitive.
It's named this way because you're basically stating that the calling thread of execution is going to wait to join the given state of execution. It's also named join in posix and many other threading packages.
After that call to join returns (unless it was interrupted), the two threads of execution are basically running together from that point (with that thread getting the return value of the now-terminated thread).
This stems from concurrent software modeling when the flow of control splits into to concurrent threads. Later, the two threads of execution will join again.
Also waitToDie() was probably a) too long and b) too morbid.
well... this isnt really correct but I thought of an "waiting room" (it actually isnt a queue with a certain scheduling as FIFO, HRRN or such).
when a thread cannot go on and needs to wait on some other thread to finish it just joins the guys (aka threads) in the waiting room to get active next...
Because you are waiting for another thread of execution (i.e. the one you're calling join on) to join (i.e. die) to the current (i.e. the calling) thread.
The calling thread does not die: it simply waits for the other thread to do so.
This is a terminology that is widely used(outside Java as well). I take it as sort of Associating a Thread with another one in some way. I think Thread.Associate() could have been a better option but Join() isn't bad either.

Resources