I'd like to combine an mce_loop with threads created by threads->create, but fail. The simplified script below never reaches the second print line. Why?
(It works if I create the threads first, but in my real script this would make other parts more complicated).
use MCE::Loop;
mce_loop { } (0);
print "OK\n";
threads->create (sub {});
print "never reached ..\n";
With the help of the (later deleted) example posted by Håkon Hægland I figured out what really helps in my simplified example. I still don't get the reason for this strange behaviour.
use MCE::Loop;
mce_loop { } (0);
MCE->new ()->run; # this, but why?
print "OK\n";
threads->create (sub {});
print "never reached ..\n";
Related
I want to check if the result from a request is having any issue. I categorize it into two: i) server error, ii) something else that is not a success. The third category is, result actually being a success. However, in the third category, I don't want to do anything.
So, my desirable code is:
if res.status().is_server_error() {
panic!("server error!");
} else if !(res.status.is_success()){
panic!("Something else happened. Status: {:?}", res.status());
} else{
pass;
}
I am aware of other ways to achieve this result: using match, ifs instead of if else if. But I wanted to learn what is the corresponding keyword of pass, like we have in Python. My aim is: if result is successful, just move along, if not, there are two ways to handle that panic.
Behold!
if predicate {
do_things();
} else {
// pass
}
Or even better
if predicate {
do_things();
} // pass
Or as I’ve recently taken to calling it the implicit + pass system
if predicate {
do_things();
}
In all seriousness there is no pass and no need for a pass in rust. As for why it exists in python, check out this answer
Python needs pass because it uses indentation-based blocks, so it requires some syntax to "do nothing". For example, this would be a syntax error in a Python program:
# syntax error - function definition cannot be empty
def ignore(_doc):
# do nothing
count = process_docs(docs, ignore) # just count the docs
The ignore function has to contain a block, which in turn must contain at least one statement. We could insert a dummy statement like None, but Python provides pass which compiles to nothing and signals the intention (to do nothing) to the human reader.
This is not needed in Rust because Rust uses braces for blocks, so one can always create an empty block simply using {}:
// no error - empty blocks are fine
fn ignore(_doc: &Document) {
// do nothing
}
let count = process_docs(docs, ignore); // just count the docs
Of course, in both idiomatic Python and Rust, one would use a closure for something as simple as the above ignore function, but there are still situations where pass and empty blocks are genuinely useful.
I remember back in college days, threads share resources and memory. I do not know the specifics of Raku implementation of threads, but if, at the same time, multiple threads call the same global function with different parameters, will they interfere one another because a global function is a single block of code shared by all the threads? E.g., this example does not show interference, but what about some complicated codes?
sub add ($a, $b) { $a + $b };
for 1..100 { start { sleep 1.rand; say "I am $_, {add($_, 1000)}"; } };
You should not have to worry about accessing a global function from multiple threads at the same time, in principle: arguments are passed by value, and parameters are lexical to the function.
There is one exception I can think of: using a state variable inside such a function. There is a known race-condition on the initialization of a state variable, and updates of the form $foo++ will most likely miss increments when being run from multiple threads at the same time. E.g.:
my int $a;
await (^10).map: { start { $a++ for ^100000 } }
say $a; # 893127
Aka, not the 1000000 you'd expect. Fortunately, to handle that case, we have atomic integers:
my atomicint $a;
await (^10).map: { start { $a⚛++ for ^100000 } }
say $a; # 1000000
But that's just showing off and not directly an answer to your question :-)
Should you have a piece of code that you want to make sure that only one thread executes at a time, you could use a Lock and the protect method on that;
my $lock = Lock.new; # usually in the mainline of a program
# ... code
$lock.protect: {
# code executed by only 1 thread at a time
}
Please note that this is considered to be "plumbing", aka use this only when you need to, as it opens you up to deadlocks.
What is the difference between if and else if in C#? For example if i write
if (x==5){
do something
}
And
else if (x==5){
do something
}
Are they totally same… so ? What is the difference?
**IF** you are confused
read the c# spec
**ELSE IF** you are kind of confused
read some books
**ELSE**
everything should be OK.
Courtesy: https://stackoverflow.com/a/1445365/5352399
Jokes are apart, usually an if statement follows this sort of structure:
if (condition)
{
// executed only if "condition" is true
}
else if (other condition)
{
// executed only if "condition" was false and "other condition" is true
}
else
{
// executed only if both "condition" and "other condition" were false
}
The if portion is the only block that is absolutely mandatory. else if allows you to say "ok, if the previous condition was not true, then if this condition is true...". The else says "if none of the conditions above were true..."
You can have multiple else if blocks, but only one if block and only one (or zero) else blocks.
Answer reference: https://stackoverflow.com/a/1439915/5352399
Please read about C# control statements that will give you comprehensive idea.
They aren't the same.
if (true)
DoSomething();
if (true)
DoSomething();
Versus
if (true)
DoSomething();
else if (true)
DoSomething();
The first example does something twice; the second does something only once.
Use else when you don't want multiple cases to be hit.
This is a little tricky, if you use plugins like resharper and you try to write a if sentence preceding of else if sentences, resharper will tell you that the 'else' in 'else if' sentence is redundant and will refactor your code to only if sentences.
Performance issue? or we really don't know how is working c# at this days?
This is a bit of an extension to a problem I posted here Pattern matching AST nodes in Rascal
I've made a bit of a modification to how the switch statement looks for patterns but it appears that none of the patterns are matching and the default pattern isn't executing!
Code:
private str synthesise_f(Core::AST::Exp exp) {
println("START");
switch (exp) {
case Exp e: str t(&T t0, &T t1) {
println("IN CASE");
}
default: println("!DEFAULT!");
}
println("END");
return ret;
}
With the output being:
START
END
Any ideas what might be happening here? Am I doing something wrong?
Thanks!
The Rascal code looks fine so I am guessing your modification (in the code of the interpreter?) is interfering with a hidden assumption or an optimization in said interpreter. Better to post a pull request on github so we can talk about it? Most likely an internal exception is causing the interpreter to skip the default case.
Suppose we are having two threads. One prints "Hello" and other prints "World". We have to manage the threads in such a way that our program should print "Hello World" five times.
Can anyone suggests me the code or pseudocode for doing this ??
Thanks in advance.
I got the answer. I have used two binary semaphores, one for each function. Let two semaphores be first and second. Initially, first = 1 & second = 0. These values are selected because we want mutual exclusion along with no deadlocks. Algorithm:
printHello()
{
wait(first)
print "Hello"
signal(second)
}
printWorld()
{
wait(second)
print " World"
signal(first)
}