pthread_cond_wait without a while loop - multithreading

global variable 'temp';
**threadA**
-pthread_mutex_lock-
if (temp == 'x')
-pthread_cond_wait-
do this
-pthread_mutex_unlock-
**threadB**
-pthread_mutex_lock-
if (someCondition == true)
temp = 'x'
-pthread_cond_signal-
-pthread_mutex_unlock-
In my case I may not have any loops, I just have an if condition. So, I want that when temp == 'x', then the threadA should do that/this.
Is the loop compulsory when dealing with the pthread_cond_wait?
What is the other way for writing the code if we don't need loops?
Is this a correct way of writing the code?

A loop is compulsory because according to http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cond_wait.html:
Spurious wakeups from the pthread_cond_timedwait() or pthread_cond_wait() functions may occur. Since the return from pthread_cond_timedwait() or pthread_cond_wait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.

Related

Can a race condition occur when executing such code on Julia?

I have a function below. Can a race condition occur when executing such code?
function thread_test(v)
Threads.#threads for i = 1:length(v)
#inbounds v[i] = rand()
end
sum(v)
end
If v is an Array there will be no race condition. Accessing different array elements in different threads is safe.
However, if v is e.g. a Dict{Int, Float64} you can have race conditions. Similarly, you are not guaranteed thread safety for subtypes of AbstractArray, like BitVector.

Julia multithreading "broadcast return"

I am new to the world of multithreading, I just want to write a function that compare (large) sorted 2D arrays:
function check_duplicates(sites, generated_structures)
for i in eachindex(generated_structures)
sites == generated_structures[i] && return false
end
return true
end
"generated_structures" might be large (5M+ elements) and to speed up things I was thinking about doing something like this:
function check_duplicates(sites, generated_structures)
Threads.#threads for i in eachindex(generated_structures)
sites == generated_structures[i] && return false
end
return true
end
So that multiple threads check subparts of the large array. Is it possible that as soon as the condition is satisfied the function stops and return false? Right now some threads return true because the subpart they checked didn't contain matches.
One way would be to have a common status flag that each thread can access and bail out if some other thread has found a match. To do this in a thread-safe manner, there is Threads.Atomic:
function hasdup(val, itr)
status = Threads.Atomic{Bool}(false)
Threads.#threads for x in itr
status[] && break
(x == val) && (status[] = true)
end
return status[]
end
Now, I'm not really sure why the access to status needs to be thread-safe, since it will only be written to in order to set it to true. Removing the Atomic wrapper still works, but is quite a bit slower.
I get a decent threading speedup from the above code, but the threading overhead is quite large, so if your match is early in the record, the threaded version will be much slower.
A multi-threading library that has much lower overhead is Polyester.jl, which will be much faster if the match comes early. It is, however, not compatible with the Threads library, so you cannot nest it within Threads.

What is the difference between a wait statement and using a while loop in SystemVerilog?

I was wondering what is the difference between waiting for a flag to be set using a wait statement or a tradition loop until the flag is set.
wait (flag === 1); //Implementation 1
while ( flag != 1); //Implementation 2
The wait statement blocks the current process until the expression becomes true.
The while loop you wrote becomes an infinite zero-delay loop if the expression is true (flag is false). This will hang simulation.

Non blocking reads with Julia

I would like to read an user input without blocking the main thread, much like the getch() function from conio.h. Is it possible in Julia?
I tried with #async but it looked like my input wasn't being read although the main thread wasn't blocked.
The problem, I believe, is either you are running on global scope which makes #async create its own local variables (when it reads, it reads into a variable in another scope) or you are using an old version of Julia.
The following examples read an integer from STDIN in a non-blocking fashion.
function foo()
a = 0
#async a = parse(Int64, readline())
println("See, it is not blocking!")
while (a == 0)
print("")
end
println(a)
end
The following two examples do the job in global scope, using an array. You can do the same trick with other types mutable objects.
Array example:
function nonblocking_readInt()
arr = [0]
#async arr[1] = parse(Int64, readline())
arr
end
r = nonblocking_readInt() # is an array
println("See, it is not blocking!")
while(r[1] == 0) # sentinel value check
print("")
end
println(r[1])

with qualifying of any of the return sattement do the loop break groovy

Do the loop terminates when it qualifies the return statement? even when value is null?
Your question is too vague to be answered with any kind of certainty. Were I to hazard a guess, however, I'd bet you're referring to control statements within a '.each' loop?
If so, the short answer is: No, return (or break) does not terminate the loop. The only way to do so is via throwing an exception within the loop à la:
try{
(1..10).each{ n->
println n
if (n == 5) throw new Exception()
}
}
catch(Exception){}
But, this is a total abomination. Use a for or while loop instead.
See also: returning from closure
If I'm off the mark, perhaps you could post some code exemplifying your topic?

Resources