protothreads-cpp icon indicating copy to clipboard operation
protothreads-cpp copied to clipboard

Suggest using volatile attribute on ptYielded

Open Xeenych opened this issue 5 years ago • 3 comments

If protothread does not have any PT_YIELD() in its Run method then compiler produces a warning, that ptYielded is set but never used.

I suggest using volatile attribute on ptYilded so compiler won't optimize (void) ptYielded; statement and won't produce any warnings.

#define PT_BEGIN() bool volatile ptYielded = true; (void) ptYielded; switch (_ptLine) { case 0:

For example I have this code without any PT_YILED()


		PT_BEGIN();
		PT_SPAWN(task1);
		PT_SPAWN(task2);
		
		readtask.Run();
		task3.Run();
		PT_WAIT_THREAD(readtask);
		PT_WAIT_THREAD(task3);
		//PT_YIELD();
		PT_END();

Xeenych avatar Jan 09 '20 13:01 Xeenych

Sorry for not responding to this earlier! I'm curious (and I haven't written a lot of C for a while): why doesn't the (void) ptYielded cause the compiler to not produce a warning here?

benhoyt avatar Sep 25 '21 05:09 benhoyt

Alternatively, you could use __attribute__((unused)).

marek22k avatar Sep 08 '23 02:09 marek22k

why doesn't the (void) ptYielded cause the compiler to not produce a warning here?

This will cast it to the type void. The compiler then seems to think that the value is used because it was casted.

marek22k avatar Sep 08 '23 02:09 marek22k