run function cpp in qml using thread - multithreading

I want to run a function that uses thread ، I'll run when the program stops a few seconds . What is the reason for this stop? In C #, to avoid this stop before I used a thread .
Please do guide me
thanks

Related

Running a task on a timer

I'd like to create a program that runs a function on an interval. I'm still very new to Elixir and do not know really where to start with this. My idea is that since we can use GenServer to create a program to sit and wait in a loop for messages, I could provide it a message (maybe :kick) and when it receives this message it would run the function.
However, that leaves one problem - how do I kick it without a cron job? Can I fire up a thread and run a timer that kicks it on an interval? If the main thread dies - is there an easy way to be notified and restart it?
Thank you!
You can use timer:send_interval/2 with a GenServer. You'll need to call the function from the init/1 callback and then handle the tick messages from the handle_info callback. Here's an example that prints 0, 1, 2, ... every second:
defmodule A do
use GenServer
def init(_) do
:timer.send_interval(1000, :tick)
{:ok, 0}
end
def handle_info(:tick, state) do
IO.inspect state
{:noreply, state + 1}
end
end
iex(1)> GenServer.start_link(A, [])
{:ok, #PID<0.94.0>}
0
1
2
3
4
...
If the main thread dies - is there an easy way to be notified and restart it?
You should look into Supervisors. The GenServer above can be added as a "worker" to a Supervisor. The Supervisor can handle restarting the GenServer if it exits for any reason.
#Dogbert mentionned using using the send_interval function from Erlang, which would be used like so : :timer.send_interval(milliseconds, process, message).
A quick Google search however, netted me the quantum-elixir library which appears to be capable of cron like scheduling, as well as scheduling tasks at runtime.

What should I do when SetEvent return false

I am writing a little program,the program execute 4 threads which will execute funcA(),I use for event objects and WaitForMultipleObject() to make the main thread block until 4 threads is over their task,it is easy to kown I will use setEvent() in funA(),I am wonder what should I do if SetEvent() is execute failed,the main will always block?Is thier any way to avoid this?
P.S:I only know SetEvent will failed when the HANDLE is invalid,is their any more reason for its failed?
In C++ you can throw a C++ exception to intentionally crash the process.
In C you can crash the process nicely using the Windows API function RaiseException to raise a Windows structured exception.

Lua Script coroutine

Hi need some help on my lua script. I have a script here that will run a server like application (infinite loop). Problem here is it doesn't execute the second coroutine.
Could you tell me whats wrong Thank you.
function startServer()
print( "...Running server" )
--run a server like application infinite loop
os.execute( "server.exe" )
end
function continue()
print("continue")
end
co = coroutine.create( startServer() )
co1 = coroutine.create( continue() )
Lua have cooperative multithreading. Threads are not swtiched automatically, but must yield to others. When one thread is running, every other thread is waiting for it to finish or yield. Your first thread in this example seems to run server.exe, which, I assume, never finishes until interrupted. Thus second thread never gets its turn to run.
You also run threads wrong. In your example you're not running any threads at all. You execute function and then would try to create coroutine with its output, which naturally would fail. But since you never get back from server.exe you didn't notice this problem yet. Remove those brackets after startServer and continue to fix it.
As already noted, there are several issues with the script that prevent you from getting what you want:
os.execute("...") is blocked until the command is completed and in your case it doesn't complete (as it runs an infinite loop). Solution: you need to detach that process from yours by using something like io.popen() instead of os.execute()
co = coroutine.create( startServer() ) doesn't create a coroutine in your case. coroutine.create call accepts a function reference and you pass it the result of startServer call, which is nil. Solution: use co = coroutine.create( startServer ) (note that parenthesis are dropped, so it's not a function call anymore).
You are not yielding from your coroutines; if you want several coroutines to work together, they need to be cooperating by giving control to each other when appropriate. That's what yield command is for and that's why it's called non-preemptive multithreading. Solution: you need to use a combination of resume and yield calls after you create your coroutine.
startServer doesn't need to be a coroutine as you are not giving control back to it; its only purpose is to start the server.
In your case, the solution may not even need coroutines as all you need to do is: (1) start the server and let it detach from your process (for example, using popen) and (2) work with your process using whatever communication protocol it requires (pipes, sockets, etc.).
There are more complex and complete solutions (like LuaLanes) and also several good descriptions on creating simple coroutine dispatchers.
Your coroutine is not yielding

Thread id of QtConcurrent run

I am doing multi-thread programs with QT.
I use this code to try whether it acts as i expected.
QFuture<void> t1 = QtConcurrent::run(thread_process1, (void *)this);
QFuture<void> t2 = QtConcurrent::run(thread_process2, (void *)this);
and both thread_process1 and 2 are only one line which is
qDebug()<<"thread id: "<<QString("%1").arg((int) QThread::currentThreadId(), 0, 16) ;
however, they both show
thread id: "ffffffffb6085b40"
am I do it wrong??
QFutureWatcher seems to do no help.
The docs for run say,
Runs function in a separate thread. The thread is taken from the
global QThreadPool. Note that the function may not run immediately;
the function will only be run when a thread is available.
There is no guarantee that each call to run will run in a different thread. It is possible that the functions run so quickly they are both handled sequentially by the same thread. Try putting in a sleep call in thread_process_1 to see if the functions are then picked up by different threads.

Jython How to stop script from thread?

I'm looking for some exit code that will be run from a thread but will be able to kill the main script. It's in Jython but I can't use java.lang.System.exit() because I still want the Java app I'm in to run, and sys.exit() isn't working. Ideally I would like to output a message then exit.
My code uses the threading.Timer function to run a function after a certain period of time. Here I'm using it to end a for loop that is executing for longer than 1 sec. Here is my code:
import threading
def exitFunct():
#exit code here
t = threading.Timer(1.0, exitFunct)
t.start()
for i in range(1, 2000):
print i
Well, if you had to, you could call mainThread.stop(). But you shouldn't.
This article explains why what you're trying to do is considered a bad idea.
If you want to kill the current process and you don't care about flushing IO buffers or reseting the terminal, you can use os._exit().
I don't know why they made this so hard.

Resources